/**
 * Created by shandianhaizan on 16/7/15.
 */

define(['validator'], function (validator) {

    function _controller($scope, $routeParams, $location, $timeout, httpService) {

        var id = $routeParams.id;
        var model ="FullCutActivity";


        var isUpdate = function(){
            return id && id!=="new"
        };

        var inArray = function(one, arr){
            return $.inArray(one, arr)>=0
        };

        var inRelation = function(one , arr){
            for(var i= 0, l=arr.length; i<l; i++){
                if(one.objectId == arr[i].objectId){
                    return true;
                }
            }
            return false;
        };



        $scope.remove = function(one, arr){
            arr.splice( $.inArray(one, arr), 1);
        };



        $scope.validation = function(arg_params){
            if(!arg_params.name) return {success:false, msg:"名称未填"};
            if(arg_params.level == 'many'){
              for(var i = 1; i < arg_params.conditions.length; i++){
                    var cs = arg_params.conditions;
                    if(!cs[i].value || !cs[i-1].value || cs[i].value <= cs[i-1].value ){
                        return {success:false, msg:"多级优惠满减价钱下一级必须大于上一级"};
                    }
                }
            };
            if(!arg_params.startTime || !arg_params.endTime || arg_params.startTime >= arg_params.endTime)   return {success:false, msg:"开始时间必须小于结束时间"};
            return {success:true}

        };




        $scope.save = function () {
            $scope.$broadcast('show-errors-check-validity');
            if (!$scope.userForm.$valid) return;
            var validation = $scope.validation($scope.activity);
            if(!validation.success) return $scope.msg.show(validation.msg);
            httpService.save(model, $scope.activity).then(function (data) {
                $scope.msg.show(data);
                if($scope.isHandleSuccess(data) && !isUpdate()){
                    $location.path('/edit-full-cut-activity/'+data.data.objectId);
                }
            });

        };


        $scope.searchProduction = function (keyword) {
            var query = {};
            var type = $scope.searchProType || "name";
            query[type] = keyword;
            return httpService.get("Production",query);
        };


        $scope.reset = function () {
            $scope.$broadcast('show-errors-reset');
            $scope.order = {};
        };


        //添加产品
        $scope.addProduction= function(c){
            if(!c || !c.objectId) return;
            $scope.activity.production =   $scope.activity.production || [];
            if(!inRelation(c, $scope.activity.production)) {
                $scope.activity.production.push({objectId: c.objectId,name: c.name});
            }
        };

        $scope.addCondition= function(){
            $scope.activity.conditions =   $scope.activity.conditions || [];
            if($scope.activity.conditions.length>5) return $scope.msg.show("最多只能创建5级");
            $scope.activity.conditions.push({});
        };

        if(isUpdate()){
            $scope.editType = "编辑";
            httpService.get(model, id).then(function (data) {
                $scope.activity = data.data;
            });
        }else{
            $scope.editType = "添加";
            $scope.activity = {level:"one",suit:"all"
            };
        }



    }

    return _controller;
});


